package com.stockz.ui.swing.charting.painter.indicator;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.List;

import com.stockz.indicator.volume.PriceRange;
import com.stockz.ui.swing.charting.display.indicator.Volume;
import com.stockz.ui.swing.charting.util.MathUtil;
import com.stockz.ui.swing.charting.view.StockChartPanelState;

public class VolumeIndicatorPainter {
	
	public static void paint(Graphics2D g, Volume display) {
		
		StockChartPanelState state = display.getStockChartPanel().getStockChartPanelState();
		Color color = new Color(0,0,255,50);
		g.setColor(color);
		
		List<PriceRange> priceRangeList = display.getPriceRangeList();
		
		if(priceRangeList == null || priceRangeList.isEmpty()){
			return;
		}
		
		double maxUpperLimit = Double.MIN_VALUE;
		double minLowerLimit = Double.MAX_VALUE;
		double maxValue = Double.MIN_VALUE;
		for(PriceRange priceRange : priceRangeList){
			if(maxUpperLimit < priceRange.getUpperLimit()){
				maxUpperLimit = priceRange.getUpperLimit();
			}
			if(minLowerLimit > priceRange.getLowerLimit()){
				minLowerLimit = priceRange.getLowerLimit();
			}
			if(maxValue < priceRange.getValue()){
				maxValue = priceRange.getValue();
			}
		}
		
		Rectangle rectangle = display.getRectangle();
		for(PriceRange priceRange : priceRangeList){
			int x = rectangle.x;
			int width = (int)(rectangle.width*priceRange.getValue()/maxValue);
			int y = MathUtil.fitY(
					rectangle.y,
					rectangle.y + rectangle.height,
					minLowerLimit,
					maxUpperLimit,
					priceRange.getUpperLimit(), 
					state.isSemiLog());
			int height = Math.abs(y - MathUtil.fitY(
					rectangle.y,
					rectangle.y + rectangle.height,
					minLowerLimit,
					maxUpperLimit,
					priceRange.getLowerLimit(), 
					state.isSemiLog()));
			g.fillRect(x, y, width, height);
		}
	}
}
